package cn.janyd.jmall.management.manager.admin;

import cn.janyd.common.core.vo.CommonResult;
import cn.janyd.jmall.management.controller.admin.dto.AdminCreateDTO;
import cn.janyd.jmall.management.controller.admin.vo.AdminVO;
import cn.janyd.jmall.management.converter.admin.AdminConverter;
import cn.janyd.jmall.system.rpc.admin.AdminRpcService;
import cn.janyd.jmall.system.rpc.admin.dto.AdminRespDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

/**
 * @author Janyd
 * @since 2020-08-15 18:21
 */
@Service
public class AdminManager {

    @DubboReference(version = "${jmall.dubbo.versions.AdminRpcService}")
    private AdminRpcService adminRpcService;

    /**
     * 获取管理员信息
     *
     * @param adminId 管理员ID
     * @return 管理员信息
     */
    public AdminVO getAdmin(Integer adminId) {
        CommonResult<AdminRespDTO> adminResult = adminRpcService.getAdmin(adminId);
        return AdminConverter.INSTANCE.convert(adminResult.getOrThrow());
    }

    /**
     * 创建管理员
     *
     * @param adminCreateDTO 创建管理员信息
     * @return 管理员信息
     */
    public AdminVO createAdmin(AdminCreateDTO adminCreateDTO) {
        CommonResult<AdminRespDTO> adminResult = adminRpcService.createAdmin(AdminConverter.INSTANCE.convert(adminCreateDTO));
        return AdminConverter.INSTANCE.convert(adminResult.getOrThrow());
    }
}
